<?php

/**
 * @author PeoneEr
 * @package aion personal cabinet v1.0
 * @copyright Aion Factory Inc. 2010-2011
 * @license GNU v3
 * @link Ai-Factory.ru && http://code.google.com/p/aionprivateofficev2
 */
require_once "core/application/models/msetparams/msetparams.php";
require_once "core/application/classes/errorClass.php";

class setParams_Controller extends SetParams_Model {

    private static $_instance = NULL;
    private $_params       = array();
    private $_lang            = NULL;

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    private function catchParams() {
        /**
         *  ловим параметры
         */
        $params = array(
            'email' => $_POST['email'],
            'question' => $_POST['question'],
            'answer' => $_POST['answer']
        );
        return $params;
    }

    public function setParams() {
        /**
         *  Устанавлиаем новые значения
         */
        $this->_lang = $this->getLanguage();
        if (strlen($_POST['question']) < 1 or strlen($_POST['question']) > 100 or strlen($_POST['answer']) < 1 or strlen($_POST['answer']) > 100) {
            $this->echoError("Слишком много символов");
        }
        $this->_params = $this->catchParams();
        try {
            foreach ($this->_params as $key => $val) {
                if (empty($val) or !isset($key)) {
                    $this->echoError($this->_lang->language['requiredFields']);
                    unset($key);
                }
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            echo $e->getLine();
            echo $e->getFile();
        }
        if ($this->issetEmail($this->_params['email']))
            $this->echoError($this->_lang->language['isseEmail']);
        try {
            $this->setNewParams($this->_params['email'], $this->_params['question'], $this->_params['answer']);
            try {
                $this->setNewParamsToSession($this->_params['email'], $this->_params['question'], $this->_params['answer']);
                $this->echoSuccess($this->_lang->language['success']);
            } catch (Exception $e) {
                echo $e->getMessage();
                error::getInstance($this->_lang->language['defaultError']);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            error::getInstance($this->_lang->language['defaultError']);
        }
    }

}

?>
